﻿using Convertor.Interface;
using System;
using System.Text.RegularExpressions;
using System.Windows;
using Wpf.CommonTools.SystemExtension;

namespace Convertor.Plugin.Compress
{
    public class Plugin : IPluginInfo, IPluginOutput, IPluginLog
    {
        #region Static Func

        static string[] LanguagePaths = new string[] { @"Convertor.Plugin.Compress;component\Assets\Lang\StringResource.zh.xaml", @"Convertor.Plugin.Compress;component\Assets\Lang\StringResource.en.xaml" };

        static Plugin()
        {
            foreach (string path in LanguagePaths)
            {
                ResourceDictionary resourceDictionary = new ResourceDictionary();
                resourceDictionary.Source = new Uri(path, UriKind.Relative);
                Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
            }
        }

        #endregion

        #region IPluginInfo 成员

        public string Name
        {
            get { return UILanguage.FindResource("PluginType_Text") + " - " + UILanguage.FindResource("Plugin_Compress_Name"); }
        }

        string IPluginInfo.Description
        {
            get { return UILanguage.FindResource("Plugin_Compress_Desc"); }
        }

        bool IPluginInfo.IsSelected { get; set; }

        int IPluginInfo.Order { get; set; }

        #endregion

        #region IPluginOutput 成员

        IOutputContent IPluginOutput.Output(IOutputContent content)
        {
            if (content == null || content.Output == null)
            {
                logTo(UILanguage.FindResource("Plugin_Common_Input_Output_Null"), LogTypes.Error);
                content.Stop = true;
                return content;
            }

            Regex reg = new Regex(@"\s+|\r");
            object value = reg.Replace(content.Output.ToString(), " ").Trim();

            logTo(UILanguage.FindResource("Plugin_Compress_ConvertSuccess"), LogTypes.Normal);

            return new OutputContent(content.Input, value);
        }

        #endregion

        #region IPluginLog 成员

        public LogDelegate Log
        {
            get;
            set;
        }

        private void logTo(string info, LogTypes logType)
        {
            if (Log != null)
            {
                Log(this, info, logType);
            }
        }

        #endregion
    }
}
